package pkg

import (
	"fmt"
	"github.com/qiniu/go-sdk/v7/auth"
	"github.com/qiniu/go-sdk/v7/storage"
	"mime/multipart"
)

func Qiniuyun(file multipart.File, size int64) string {
	bucket := "laomachi"
	putPolicy := storage.PutPolicy{
		Scope: bucket,
	}
	mac := auth.New("c2RytiFZrxjm8CF4ZpPrbhBdEUR_MIb8gJKvRKpN", "rus_ItGAPWc9VQrZizz7abgBvGzhBUyQczYR4Yzv")
	upToken := putPolicy.UploadToken(mac)
	cfg := storage.Config{}
	// 空间对应的机房
	cfg.Region = &storage.ZoneHuadong
	// 是否使用https域名
	cfg.UseHTTPS = true
	// 上传是否使用CDN上传加速
	cfg.UseCdnDomains = false
	// 构建表单上传的对象
	formUploader := storage.NewFormUploader(&cfg)
	ret := storage.PutRet{}
	// 可选配置
	putExtra := storage.PutExtra{
		Params: map[string]string{
			"x:name": "github logo",
		},
	}
	err := formUploader.PutWithoutKey(nil, &ret, upToken, file, size, &putExtra)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	fmt.Println(ret.Key, ret.Hash)
	imageUrl := "http://sivcd2hj8.hn-bkt.clouddn.com" + ret.Key
	return imageUrl
}
